//login.js
const WXAPI = require('../../wxapi/main')
const CONFIG = require('../../config.js')
//获取应用实例
var app = getApp();
Page({
  data: {
    remind: '加载中',
    angle: 0
  },
  goToIndex:function(){
    wx.switchTab({
      url: '/pages/index/index',
    });
  },
  onLoad:function(options){
    this.checkLoginStatus();
    console.log("upper_fSid--" + options.upper_fSid);

    if (typeof (options.upper_fSid) != "undefined" && options.fSid != "") {
      wx.setStorageSync('upper_fSid',options.upper_fSid);
      console.log("9999--" + options.upper_fSid);
    }else{
      wx.setStorageSync('upper_fSid',"");
    }
    console.log("00000--" + options.upper_fSid);
    wx.setStorageSync("discount","");
  },
  onShow:function(){

  },
  onReady: function(){
    var that = this;
    setTimeout(function(){
      that.setData({
        remind: ''
      });
    }, 1000);
    wx.onAccelerometerChange(function(res) {
      var angle = -(res.x*30).toFixed(1);
      if(angle>14){ angle=14; }
      else if(angle<-14){ angle=-14; }
      if(that.data.angle !== angle){
        that.setData({
          angle: angle
        });
      }
    });
  },
  //登录校验
  checkLoginStatus() { // 检测登录状态
    const _this = this
    const token = wx.getStorageSync('token');
    if (!token) {
      _this.goLoginPageTimeOut()
      return
    }
    WXAPI.checkToken(token).then(function (res) {
      if (res.code != 0) {
        wx.removeStorageSync('token')
        _this.goLoginPageTimeOut()
        return
      }
    })
    wx.checkSession({
      fail() {
        _this.goLoginPageTimeOut()
        return
      }
    })

    // 已经处于登录状态，检测是否强制需要手机号码
    if (CONFIG.requireBindMobile) {
      WXAPI.userDetail(token).then(function (res) {
        if (res.code == 0) {
          if (!res.data.base.mobile) {
            wx.navigateTo({
              url: "/pages/authorize/bindmobile"
            })
          }
        }
      })
    }
  },
  goLoginPageTimeOut: function () {
    if (this.navigateToLogin) {
      return
    }
    wx.removeStorageSync('token')
    this.navigateToLogin = true
    setTimeout(function () {
      wx.navigateTo({
        url: "/pages/authorize/index"
      })
    }, 1000)
  },
});